using UnityEngine;
using System.Collections;

public abstract class BotBase : MonoBehaviour 
{
    public BotSensor[] sensors  = null;

    public float lookDistance   = 500f;
    public float atackDistance  = 10f;

    private SmoothLookAt lookAt;
    protected GameObject lookTarget;

    protected BotBaseMotor botMotor;

    void Awake()
    {
        lookTarget = new GameObject();

        sensors = CreateSensors();

        gameObject.AddComponent<SmoothLookAt>();
        lookAt = gameObject.GetComponent<SmoothLookAt>();
        lookAt.target = lookTarget.transform;
        lookAt.damping = 4f;
        lookAt.smooth = true;
    }

    void Start()
    {
        botMotor = GetMotor();
    }
	
	void FixedUpdate () {

        GameObject stimulus = GetBestStimulus();

        if (stimulus != null)
        {
            Vector3 from = transform.position;
            Vector3 to = stimulus.transform.position;
            Debug.DrawLine(from, to, Color.magenta);

            MoveTo(stimulus);

            if (IsNearAt(stimulus))
                AttackThe(stimulus);
        }
        else
            LoseTarget();
    }

    protected GameObject GetBestStimulus()
    {
        GameObject stimulus = null;
        foreach (BotSensor sensor in sensors)
        {
            stimulus = sensor.GetStimulus();
            if (stimulus != null)
                break;
        }
        return stimulus;
    }

    protected abstract BotSensor[] CreateSensors();
    protected abstract BotBaseMotor GetMotor();
    protected abstract void MoveTo(GameObject obj);
    protected abstract void RunTo(GameObject obj);
    protected abstract void LoseTarget();
    protected abstract bool IsNearAt(GameObject obj);
    protected abstract void AttackThe(GameObject obj);
}
